package com.dao;

import com.entity.game;

import java.util.List;

public class goodsmanagedapimpl implements goodsmanagedao{

    @Override
    public int insert(String fgame, String sgame,String account,String password,String value,String price,String zone,String UserID,String Deposit,String Syste,String Title,String Level,String Pname) {
        //根据网页带回的信息增加作为商品的游戏账号，判断是否已存在或者要上架的游戏暂不支持，
        //游戏已存在或者不支持该游戏返回false，并交由系统提示,
        //flag=0,上架成功，flag=1，不支持该游戏，flag=2，游戏已上架
        int flag=3;
        randomid r=new randomid();
        String ProductID=r.randomid();
        String PDID=r.randomid();
        String gID="";
        String sql2="select * from game where GameName=?";
        UserInfDao u=new UserInfDao();
        List<game> list1 =u.getForList(game.class,sql2,sgame);
        for (int i = 0; i < list1.size(); i++) {
            if(sgame.equals(list1.get(i).getGameName()))
            {
                gID=list1.get(i).getGameID();
                break;
            }
        }
        if(!("".equals(fgame))&&!("".equals(sgame))&&!("".equals(account))&&!("".equals(zone))) {
            judge j = new judgeimpl();
            if (j.judgegame(fgame, sgame)) {
                if (j.judgeisonshelve(sgame, account, zone)) {
                    String sql = "insert into productdetails(PDID,ProductID,Account,Pwd,ProductValue,UnitPrice,zone,Deposit,Syste,Title,Level,Pname) values (?,?,?,?,?,?,?,?,?,?,?,?)";
                    String sql1 = "insert into product values(?,?,?,?,?,?)";
                    //UserInfDao u=new UserInfDao();
                    u.update(sql, PDID, ProductID, account, password, value, price, zone, Deposit, Syste, Title, Level, Pname);
                    u.update(sql1, ProductID, 1, UserID, PDID, gID, 0);
                } else {
                    flag = 2;
                }
            } else {
                flag = 1;
            }
        }
        else
        {
            flag=4;
        }
        return flag;
    }

    @Override
    public boolean delete(String account) {
        boolean flag2=false;
        judge j1=new judgeimpl();
        if(j1.judgeisexist(account))
        {
            String sql2="delete from productdetails where account =?";
            UserInfDao u=new UserInfDao();
            u.update(sql2,account);
            flag2=true;
        }
        return flag2;
    }

    @Override
    public boolean update(String account,String value,String zone,String price) {
        boolean flag3=false;
        judge j2=new judgeimpl();
        if(j2.judgeisexist(account))
        {
            String sql3="update productdetails set value=?,zone=?,price=? where account =?";
            UserInfDao u=new UserInfDao();
            u.update(sql3,value,zone,price,account);
            flag3=true;
        }
        return flag3;
    }
}
